import 'package:flutter/material.dart';

class DataTablePage extends StatefulWidget {
  final Map<String, dynamic> arguments;
  DataTablePage({Key key, this.arguments}) : super(key: key);

  @override
  _DataTablePageState createState() => _DataTablePageState();
}

class _DataTablePageState extends State<DataTablePage> {
  List<BookModel> _bookModels;

  bool _sortAscending = false;
  int _sortColumnIndex = 1;
  bool _editing = false;
  BookDataSource _bookDataSource;

  @override
  void initState() {
    super.initState();

    final List<BookModel> bookModels = bookMaps.map((map) {
      return BookModel.fromMap(map);
    }).toList();

    this._bookModels = bookModels;

    this._bookDataSource = BookDataSource(bookModels);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DataTablePage'),
        actions: <Widget>[
          IconButton(
              icon: Icon(Icons.edit),
              onPressed: () {
                setState(() {
                  this._editing = !this._editing;

                  // 取消编辑后 取消全部选中
                  if (this._editing == false) {
                    this._bookModels.forEach((m) {
                      m.selected = false;
                    });
                  }
                });
              }),
        ],
      ),
      body: ListView(
        children: <Widget>[
          Text('DataTable'),
          DataTable(
            sortAscending: this._sortAscending,
            sortColumnIndex: this._sortColumnIndex,
            onSelectAll: (bool select) {
              setState(() {
                this._bookModels.forEach((m) {
                  m.selected = select;
                });
              });
            },
            columns: [
              DataColumn(label: Text('id')),
              DataColumn(
                label: Text('title'),
                onSort: (int columnIndex, bool ascending) {
                  setState(() {
                    this._sortColumnIndex = columnIndex;
                    this._sortAscending = ascending;

                    _bookModels.sort((a, b) {
                      if (ascending) {
                        return a.title.compareTo(b.title);
                      } else {
                        return b.title.compareTo(a.title);
                      }
                    });
                  });
                },
              ),
              DataColumn(label: Text('author')),
            ],
            rows: this._bookModels.map((m) {
              return DataRow(
                onSelectChanged: (() {
                  if (this._editing) {
                    return (select) {
                      setState(() {
                        m.selected = select;
                      });
                    };
                  } else {
                    return null;
                  }
                })(),
                cells: [
                  DataCell(Text(m.id)),
                  DataCell(Text(m.title)),
                  DataCell(Text(m.author)),
                ],
                selected: m.selected,
              );
            }).toList(),
          ),
          Text('PaginatedDataTable'),
          PaginatedDataTable(
              headingRowHeight: 30.0,
              horizontalMargin: 0.0,
              columnSpacing: 20.0,
              rowsPerPage: 6,
              header: Text('Books'),
              columns: [
                DataColumn(label: Text('imageUrl')),
                DataColumn(label: Text('id')),
                DataColumn(label: Text('title')),
                DataColumn(label: Text('author')),
                DataColumn(label: Text('description')),
              ],
              source: this._bookDataSource),
        ],
      ),
    );
  }
}

/// BookModel
class BookModel {
  final String id;
  final String title;
  final String author;
  final String description;
  final String imageUrl;

  bool selected = false;

  BookModel({this.id, this.title, this.author, this.description, this.imageUrl});
  BookModel.fromMap(Map<String, dynamic> map)
      : this.id = map['id'].toString(),
        this.title = map['title'].toString(),
        this.author = map['author'].toString(),
        this.description = map['description'].toString(),
        this.imageUrl = map['imageUrl'].toString();
}

/// BookDataSource
class BookDataSource extends DataTableSource {
  final List<BookModel> _bookModels;

  BookDataSource(this._bookModels);

  int _selectedRowCount = 0;

  @override
  DataRow getRow(int index) {
    final m = this._bookModels[index];
    return DataRow(
      cells: [
        DataCell(
          Container(
            width: 60.0,
            color: Colors.red,
            child: Center(
              child: Image.network(m.imageUrl),
            ),
          ),
        ),
        DataCell(
          Container(
            width: 30.0,
            child: Text(m.id),
          ),
        ),
        DataCell(Text(m.title)),
        DataCell(Text(m.author)),
        DataCell(Text(m.description)),
      ],
      selected: m.selected,
    );
  }

  @override
  bool get isRowCountApproximate => false;

  @override
  int get rowCount => this._bookModels.length;

  @override
  int get selectedRowCount => _selectedRowCount;
}

List<Map<String, dynamic>> bookMaps = [
  {
    "id": 1,
    "title": "Candy Shop",
    "author": "Mohamed Chahin",
    "description":
        "Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.",
    "imageUrl": "https://resources.ninghao.net/images/candy-shop.jpg"
  },
  {
    "id": 2,
    "title": "Childhood in a picture",
    "author": "Mohamed Chahin",
    "description":
        "Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.",
    "imageUrl": "https://resources.ninghao.net/images/childhood-in-a-picture.jpg"
  },
  {
    "id": 3,
    "title": "Contained",
    "author": "Mohamed Chahin",
    "description":
        "Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.",
    "imageUrl": "https://resources.ninghao.net/images/contained.jpg"
  },
  {
    "id": 4,
    "title": "Dragon",
    "author": "Mohamed Chahin",
    "description":
        "Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.",
    "imageUrl": "https://resources.ninghao.net/images/dragon.jpg"
  },
  {
    "id": 5,
    "title": "Free Hugs",
    "author": "Mohamed Chahin",
    "description":
        "Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.",
    "imageUrl": "https://resources.ninghao.net/images/free_hugs.jpg"
  },
  {
    "id": 6,
    "title": "Gravity Falls",
    "author": "Mohamed Chahin",
    "description":
        "Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.",
    "imageUrl": "https://resources.ninghao.net/images/gravity-falls.png"
  },
  {
    "id": 7,
    "title": "Icecream Truck",
    "author": "Mohamed Chahin",
    "description":
        "Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.",
    "imageUrl": "https://resources.ninghao.net/images/icecreamtruck.png"
  },
  {
    "id": 8,
    "title": "keyclack",
    "author": "Mohamed Chahin",
    "description":
        "Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.",
    "imageUrl": "https://resources.ninghao.net/images/keyclack.jpg"
  },
  {
    "id": 9,
    "title": "Overkill",
    "author": "Mohamed Chahin",
    "description":
        "Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.",
    "imageUrl": "https://resources.ninghao.net/images/overkill.png"
  },
  {
    "id": 10,
    "title": "Say Hello to Barry",
    "author": "Mohamed Chahin",
    "description":
        "Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.",
    "imageUrl": "https://resources.ninghao.net/images/say-hello-to-barry.jpg"
  },
  {
    "id": 11,
    "title": "Space Skull",
    "author": "Mohamed Chahin",
    "description":
        "Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.",
    "imageUrl": "https://resources.ninghao.net/images/space-skull.jpg"
  },
  {
    "id": 12,
    "title": "The Old Fashioned",
    "author": "Mohamed Chahin",
    "description":
        "Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.",
    "imageUrl": "https://resources.ninghao.net/images/the-old-fashioned.png"
  },
  {
    "id": 13,
    "title": "Tornado",
    "author": "Mohamed Chahin",
    "description":
        "Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.",
    "imageUrl": "https://resources.ninghao.net/images/tornado.jpg"
  },
  {
    "id": 14,
    "title": "Undo",
    "author": "Mohamed Chahin",
    "description":
        "Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.",
    "imageUrl": "https://resources.ninghao.net/images/undo.jpg"
  },
  {
    "id": 15,
    "title": "White Dragon",
    "author": "Mohamed Chahin",
    "description":
        "Esse ut nulla velit reprehenderit veniam sint nostrud nulla exercitation ipsum. Officia deserunt aliquip aliquip excepteur eiusmod dolor. Elit amet ipsum labore sint occaecat dolore tempor officia irure voluptate ad. Veniam laboris deserunt aute excepteur sit deserunt dolor esse dolor velit sint nulla anim ut. Reprehenderit voluptate adipisicing culpa magna ea nulla ullamco consectetur. Cupidatat adipisicing consequat adipisicing sit consectetur dolor occaecat.",
    "imageUrl": "https://resources.ninghao.net/images/white-dragon.jpg"
  }
];
